﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using BidFunLib.Entity;
using BidFunLib.Services;

namespace BidFunLib.EntityHelper
{
    public class BidSaleHelper
    {

        private static readonly ILog log = LogManager.GetLogger(typeof(BidSaleHelper));

        /// <summary>
        /// Singleton instance
        /// </summary>
        private static BidSaleHelper instance;

        /// <summary>
        /// Retreive singleton of this class
        /// </summary>
        public static BidSaleHelper Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new BidSaleHelper();
                }
                return BidSaleHelper.instance;
            }
            set
            {
                BidSaleHelper.instance = value;
            }
        }

        private BidSaleHelper()
        {
        }


        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public Int32 getNbBidByBot(BidSale bidSale)
        {
            int nbBidBot = 0;
            foreach (BidLogger bLogger in bidSale.BidloggerList)
            {
                if (bLogger.BidOperatorType.IdBidOperatorType == BidOperatorTypeManager.Instance.OperatorTypeAutomatic.IdBidOperatorType)
                {
                    nbBidBot++;
                }
            }
            log.Info("BidSale " + bidSale.IdBidSale + " Nb bid automatic = " + nbBidBot);
            return nbBidBot;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public Int32 getNbBidManual(BidSale bidSale)
        {
            int nbBidManual = 0;
            foreach (BidLogger bLogger in bidSale.BidloggerList)
            {
                if (bLogger.BidOperatorType.IdBidOperatorType == BidOperatorTypeManager.Instance.OperatorTypeManual.IdBidOperatorType)
                {
                    nbBidManual++;
                }
            }
            log.Info("BidSale " + bidSale.IdBidSale + " Nb bid manual = " + nbBidManual);
            return nbBidManual;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public Int32 getNbPointsPaidByWinner(BidSale bidSale)
        {
            Int32 nbPointsPaidByWinner = 0;
            Int32 idBidUserWinner = BidSaleHelper.Instance.getWinningBidLogger(bidSale).IdBidUser;
            foreach (BidLogger bLogger in bidSale.BidloggerList)
            {
                if (bLogger.IdBidUser == idBidUserWinner) 
                {
                    nbPointsPaidByWinner++;
                }
            }
            log.Info("BidSale " + bidSale.IdBidSale + " Winner " + idBidUserWinner + " Nb Points = " + nbPointsPaidByWinner);
            return nbPointsPaidByWinner;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public BidLogger getWinningBidLogger(BidSale bidSale)
        {
            //bidSale.BidloggerList
            foreach (BidLogger bLogger in bidSale.BidloggerList)
            {
                if (bLogger.Winner) 
                {
                    return bLogger;
                }
            }
            return null;
        }

    }
        
}
